package com.kakalog.main.controller;

import com.kakalog.main.entity.ChartData;
import com.kakalog.main.entity.ChartMonth;
import com.kakalog.main.entity.RingChartData;
import com.kakalog.main.entity.RingChartValueData;
import com.kakalog.main.service.SysDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author kaka.huang
 * @for study
 */
//这是图表方法控制层
@RestController
public class SysChartsController {
    @Autowired
    SysDetailService sysDetailService;

    @PostMapping("/getWeekChartData")
    public Map<String, Object> getWeekChartData(@RequestBody ChartData chartData) {
        Map<String,Object> dataMap = new HashMap<>();
        String[] dayList = chartData.getDayList();

        String start_date = dayList[0];
        String end_date = dayList[6];
        String totalzhichuStr = sysDetailService.selectZhiChuData(start_date,end_date,chartData.getUid());
        String totalshouruStr = sysDetailService.selectShouRuData(start_date,end_date,chartData.getUid());

        try {
            double Dtotalzhichu = (totalzhichuStr != null) ? Double.parseDouble(totalzhichuStr) / 100.0 : 0.0;
            double Dtotalshouru = (totalshouruStr != null) ? Double.parseDouble(totalshouruStr) / 100.0 : 0.0;

            String totalzhichu = String.format("%.2f", Dtotalzhichu);
            String totalshouru = String.format("%.2f", Dtotalshouru);

            double[] data = new double[dayList.length];

            for (int i = 0; i < dayList.length; i++) {
                if (chartData.getZhichushourutype() == 1){
                    String s = sysDetailService.selectZhiChuByUidAndDate(chartData.getUid(), dayList[i]);
                    double sum = (s != null) ? Double.parseDouble(s) / 100.0 : 0.0;
                    data[i] = -sum;
                }else{
                    String s = sysDetailService.selectShouRuByUidAndDate(chartData.getUid(), dayList[i]);
                    double sum = (s != null) ? Double.parseDouble(s) / 100.0 : 0.0;
                    data[i] = sum;
                }
            }
            dataMap.put("chartData",data);
//            获取环形表数据
            if (chartData.getZhichushourutype() == 1){
                List<RingChartValueData> ringChartData = sysDetailService.selectZhiChuRingData(start_date, end_date, chartData.getUid());
                System.out.println(ringChartData);
                dataMap.put("ringData",ringChartData);
            }else{
                List<RingChartValueData> ringChartData = sysDetailService.selectShouRuRingData(start_date, end_date, chartData.getUid());

                System.out.println(ringChartData);
                dataMap.put("ringData",ringChartData);
            }

            dataMap.put("totalshouru",totalshouru);
            dataMap.put("totalzhichu",totalzhichu);

            return dataMap;

        } catch (Exception e) {
            // 记录详细的错误信息并返回合适的响应

            return null;
        }
    }

    @PostMapping("getMonthChartData")
    public Map<String, Object> getMonthChartData(@RequestBody ChartMonth chartMonth){
        System.out.println("连接成功"+chartMonth);
        Map<String,Object> dataMap = new HashMap<>();

//        1.解析字符串，使202407格式的时间得到月份07，并将这个月的每一天保存在数组中
        // 解析年和月
        int year = Integer.parseInt(chartMonth.getYearmonth().substring(0, 4));
        int month = Integer.parseInt(chartMonth.getYearmonth().substring(4, 6));

        // 确保月份合法
        if (month < 1 || month > 12) {
            throw new IllegalArgumentException("Invalid month: " + month);
        }

        // 生成该月的每一天的“日”部分
        // 使用指定的年月来计算最大天数
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1); // 设置为该月的第一天
        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        String[] days = new String[maxDay];  // 根据实际获取的天数来初始化数组

        for (int day = 1; day <= maxDay; day++) {
            // 格式化日期为“日”部分
            String dayStr = String.format("%02d", day); // 确保两位数
            days[day - 1] = dayStr; // day - 1 是因为数组索引从 0 开始
        }

        System.out.println(days);
        dataMap.put("days",days);
//        日期数组
        String start_date = chartMonth.getYearmonth() + days[0];
        String end_date = chartMonth.getYearmonth() + days[days.length-1];
        String totalzhichuStr = sysDetailService.selectZhiChuData(start_date,end_date,chartMonth.getUid());
        String totalshouruStr = sysDetailService.selectShouRuData(start_date,end_date,chartMonth.getUid());
        try {
            double Dtotalzhichu = (totalzhichuStr != null) ? Double.parseDouble(totalzhichuStr) / 100.0 : 0.0;
            double Dtotalshouru = (totalshouruStr != null) ? Double.parseDouble(totalshouruStr) / 100.0 : 0.0;

            String totalzhichu = String.format("%.2f", Dtotalzhichu);
            String totalshouru = String.format("%.2f", Dtotalshouru);

            double[] data = new double[days.length];
            for (int i = 0; i < days.length; i++) {
                String dayList = chartMonth.getYearmonth()+days[i];
                if (chartMonth.getZhichushourutype() == 1){
                    String s = sysDetailService.selectZhiChuByUidAndDate(chartMonth.getUid(), dayList);
                    double sum = (s != null) ? Double.parseDouble(s) / 100 : 0;
                    data[i] = -sum;
                }else{
                    String s = sysDetailService.selectShouRuByUidAndDate(chartMonth.getUid(), dayList);
                    double sum = (s != null) ? Double.parseDouble(s) / 100 : 0;
                    data[i] = sum;
                }


            }
            dataMap.put("chartData",data);
            dataMap.put("totalshouru",totalshouru);
            dataMap.put("totalzhichu",totalzhichu);

            //            获取环形表数据
            if (chartMonth.getZhichushourutype() == 1){
                List<RingChartValueData> ringChartData = sysDetailService.selectZhiChuRingData(start_date, end_date, chartMonth.getUid());
                System.out.println(ringChartData);
                dataMap.put("ringData",ringChartData);
            }else{
                List<RingChartValueData> ringChartData = sysDetailService.selectShouRuRingData(start_date, end_date, chartMonth.getUid());

                System.out.println(ringChartData);
                dataMap.put("ringData",ringChartData);
            }

        }catch (Exception e){

        }

        return dataMap;
    }

    @PostMapping("/getYearChartData")
    public Map<String, Object> getYearChartData(@RequestBody ChartMonth chartMonth) {
        System.out.println("连接成功" + chartMonth);
        Map<String, Object> dataMap = new HashMap<>();
        Integer[] months = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
        String start_date = chartMonth.getYearmonth() + "0101";
        String end_date = chartMonth.getYearmonth() + "1231";
        double[] data = new double[months.length];
        String totalzhichuStr = sysDetailService.selectZhiChuData(start_date,end_date,chartMonth.getUid());
        String totalshouruStr = sysDetailService.selectShouRuData(start_date,end_date,chartMonth.getUid());

        try {
            // 使用 Double 类型来处理两位小数
            double Dtotalzhichu = (totalzhichuStr != null) ? Double.parseDouble(totalzhichuStr) / 100.0 : 0.0;
            double Dtotalshouru = (totalshouruStr != null) ? Double.parseDouble(totalshouruStr) / 100.0 : 0.0;

            String totalzhichu = String.format("%.2f", Dtotalzhichu);
            String totalshouru = String.format("%.2f", Dtotalshouru);

            for (int i = 0; i < months.length; i++) {
                String formattedMonth = String.format("%02d", months[i]);
                String yearMonth = chartMonth.getYearmonth() + formattedMonth;
                System.out.println(yearMonth);

                if (chartMonth.getZhichushourutype() == 1){
                    String s = sysDetailService.selectDateSumByUidAndMonthDateZ(chartMonth.getUid(), yearMonth);
                    double sum = (s != null) ? Double.parseDouble(s) / 100.0 : 0.0;
                    data[i] = -sum;
                }else{
                    String s = sysDetailService.selectDateSumByUidAndMonthDateS(chartMonth.getUid(), yearMonth);
                    double sum = (s != null) ? Double.parseDouble(s) / 100.0 : 0.0;
                    data[i] = sum;
                }

            }




            dataMap.put("months", months);
            dataMap.put("chartData", data);
            dataMap.put("totalshouru", totalshouru);
            dataMap.put("totalzhichu", totalzhichu);

            List<RingChartValueData> ringChartData;
            if (chartMonth.getZhichushourutype() == 1) {
                ringChartData = sysDetailService.selectZhiChuRingData(start_date, end_date, chartMonth.getUid());
            } else {
                ringChartData = sysDetailService.selectShouRuRingData(start_date, end_date, chartMonth.getUid());
            }
            System.out.println(ringChartData);
            dataMap.put("ringData", ringChartData);

            return dataMap;
        } catch (NumberFormatException e) {
            System.err.println("数据转换错误: " + e.getMessage());
            return Collections.singletonMap("error", "数据格式错误");
        } catch (Exception e) {
            System.err.println("发生异常: " + e.getMessage());
            return Collections.singletonMap("error", "内部服务器错误");
        }
    }
}
